package com.sisgesbyte.dao.seguridad;

import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.sisgesbyte.dao.beanDao.FiltrosSQLBean;
import com.sisgesbyte.dao.beanDao.RolTablaDetRequest;
import com.sisgesbyte.dao.utilDao.CreaFiltroSqlString;
import com.sisgesbyte.data.entity.comun.GenericEntity;
import com.sisgesbyte.data.entity.seguridad.RolTablaListaEntity;
import com.sisgesbyte.data.util.AbstractFacade;

@Stateless
public class MantenimientoRolTablaDaoImpl extends
		AbstractFacade<RolTablaListaEntity> implements MantenimientoRolTablaDao {

	@Inject
	private CreaFiltroSqlString creaFiltroSqlString;
	
	@PersistenceContext(unitName = "SigesSiscompPU")
	private EntityManager em;

	@Override
	protected EntityManager getEntityManager() {
		return em;
	}

	public MantenimientoRolTablaDaoImpl() {
		super(RolTablaListaEntity.class);
	}

	@Override
	public List<RolTablaListaEntity> listaRolTabla(FiltrosSQLBean cadenaFiltros) {
		StringBuilder jpql = new StringBuilder();
		jpql.append("select rt.id, rt.rol_id, r.nombre as rol,rt.tablas_sistema_id as cod_tabla , ts.nombre as tabla, rt.ver, rt.actualizar, rt.eliminar, rt.active from seguridad.rol_tabla rt ");
		jpql.append("inner join seguridad.tablas_sistema ts on rt.tablas_sistema_id=ts.id ");
		jpql.append("inner join seguridad.rol r on rt.rol_id=r.id ");
		// concatenamos el filtro
		if (cadenaFiltros != null) {
			StringBuilder filtro = new StringBuilder();
			filtro.append(creaFiltroSqlString.concatenar(cadenaFiltros));
			jpql.append(filtro.toString());
		}
		Query query = em.createNativeQuery(jpql.toString(),
				RolTablaListaEntity.class);
		if (cadenaFiltros != null && cadenaFiltros.getListaValores() != null
				&& cadenaFiltros.getListaValores().size() > 0) {
			int num = 0;
			for (Object valor : cadenaFiltros.getListaValores()) {
				query.setParameter(++num, valor);
			}
		}
		List<RolTablaListaEntity> ls = query.getResultList();

		return ls;
	}

	@Override
	public GenericEntity guardarRolTabla(RolTablaDetRequest request) {

		StringBuilder jpql = new StringBuilder();
		jpql.append("select * from seguridad.sp_insertar_rol_tabla(:cod_rol, :cod_tabla, :ver, :actualizar, :eliminar, :active)");

		Query query = em
				.createNativeQuery(jpql.toString(), GenericEntity.class);
		query.setParameter("cod_rol", request.getCodRol());
		query.setParameter("cod_tabla", request.getCodTabla());
		query.setParameter("ver", request.getVer());
		query.setParameter("actualizar", request.getActualizar());
		query.setParameter("eliminar", request.getEliminar());
		query.setParameter("active", request.getActive());

		return (GenericEntity) query.getSingleResult();
	}

	@Override
	public GenericEntity actualizarRolTabla(RolTablaDetRequest request) {
		StringBuilder jpql = new StringBuilder();
		jpql.append("select * from seguridad.sp_actualizar_rol_tabla(:in_roltablaid, :in_rolid, :in_tablaid, :in_ver, :in_actualizar, :in_eliminar, :in_active)");

		Query query = em
				.createNativeQuery(jpql.toString(), GenericEntity.class);
		query.setParameter("in_roltablaid", request.getCodRolTabla());
		query.setParameter("in_rolid", request.getCodRol());
		query.setParameter("in_tablaid", request.getCodTabla());
		query.setParameter("in_ver", request.getVer());
		query.setParameter("in_actualizar", request.getActualizar());
		query.setParameter("in_eliminar", request.getEliminar());
		query.setParameter("in_active", request.getActive());

		return (GenericEntity) query.getSingleResult();
	}

	@Override
	public GenericEntity eliminarRolTabla(int codRolTabla) {

		StringBuilder jpql = new StringBuilder();
		jpql.append("select * from  seguridad.sp_eliminar_rol_tabla(:in_roltablaid)");

		Query query = em
				.createNativeQuery(jpql.toString(), GenericEntity.class);
		query.setParameter("in_roltablaid", codRolTabla);

		return (GenericEntity) query.getResultList();
	}

}
